package com.ang.hutool.excel.simpleDemos;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ssang
 * @date 2021-09-04 15:31
 */
public class ReaderExcel {
    public static void main(String[] args) {
        ReaderExcel readerExcel = new ReaderExcel();
        readerExcel.readExcle();
    }

    public void readExcle(){
        ExcelReader reader = ExcelUtil.getReader("D:/read.xlsx");

        /**
         *  Excel数据的格式
         *  标题0    标题1    标题2    标题3
         *  AAA0     BBB0    CCC0     DDD0
         *  AAA1     BBB1    CCC1     DDD1
         *  AAA2     BBB2    CCC2     DDD2
         */

        // 直接按照行读取所有行的数据
        List<List<Object>> read = reader.read();
        read.stream().forEach(objects -> System.out.println(objects));

        System.out.println("-----------------");

        // 直接从第3行开始读取
        List<List<Object>> read1 = reader.read(2);
        read1.stream().forEach(objects -> System.out.println(objects));

        System.out.println("-----------------");

        // 第一行的列名 ——> 接下来的列的对应值
        List<Map<String, Object>> maps = reader.readAll();
        maps.stream().forEach(map -> map.entrySet().stream().forEach(item -> System.out.println("Column: "+item.getKey()+" Value: "+item.getValue())));

        System.out.println("-----------------");

        // 根据实体类进行字段的对应
        // 直接转换，第一行的标题必须和实体类中的属性一直，如下：
        /**
         * name    age    pass    socre     examDate
         * A       1      true     1        2020-02-02
         * B       2      true     2        2020-02-02
         * C       3      false    3        2020-02-02
         */
        List<TestBean> students = reader.readAll(TestBean.class);
        students.stream().forEach(student -> System.out.println(student));

    }

    class TestBean{
        private String name;
        private int age;
        private boolean pass;
        private double score;
        private Date examDate;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public boolean isPass() {
            return pass;
        }

        public void setPass(boolean pass) {
            this.pass = pass;
        }

        public double getScore() {
            return score;
        }

        public void setScore(double score) {
            this.score = score;
        }

        public Date getExamDate() {
            return examDate;
        }

        public void setExamDate(Date examDate) {
            this.examDate = examDate;
        }
    }
}
